#!/bin/python

#
#Xithyl, Test Driven Development for C
#Copyright (C) 2012  Stepan Henek
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os

from initialize import *
from tests import *
from cleanup import *

from xithyl import logger
from xithyl.utils import fork

error=""

logger.addLogger(__name__)

def StartFunctional():

    def perform(expectedRetVal, function):
        ret = fork.runSafe(function, True)
        if ret != expectedRetVal:
            error += "Function \"%s\" failed." % function.__name__
            return False
        return True

    def memchr():

        objects = initString()
        testMemchrFunctional(objects)
        cleanString(objects)

        return True

    def memcmp():

        objects = initString()
        testMemcmpFunctional(objects)
        cleanString(objects)

        return True

    def memcpy():

        objects = initString()
        testMemcpyFunctional(objects)
        cleanString(objects)

        return True

    def memmove():

        objects = initString()
        testMemmoveFunctional(objects)
        cleanString(objects)

        return True

    def memset():

        objects = initString()
        testMemsetFunctional(objects)
        cleanString(objects)

        return True

    def strcat():

        objects = initString()
        testStrcatFunctional(objects)
        cleanString(objects)

        return True

    def strncat():

        objects = initString()
        testStrncatFunctional(objects)
        cleanString(objects)

        return True

    def strchr():

        objects = initString()
        testStrchrFunctional(objects)
        cleanString(objects)

        return True

    def strcmp():

        objects = initString()
        testStrcmpFunctional(objects)
        cleanString(objects)

        return True

    global error
    result = True

    result &= perform(os.EX_OK, memchr)
    result &= perform(os.EX_OK, memcmp)
    result &= perform(os.EX_OK, memcpy)
    result &= perform(os.EX_OK, memmove)
    result &= perform(os.EX_OK, memset)
    result &= perform(os.EX_OK, strcat)
    result &= perform(os.EX_OK, strncat)
    result &= perform(os.EX_OK, strchr)
    result &= perform(os.EX_OK, strcmp)

    return result

def ErrorMsg():
    global error
    return error

